package com.macro.mall.controller;

import cn.hutool.core.util.StrUtil;
import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.model.UmsBestVolunteer;
import com.macro.mall.service.UmsBestVolunteerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 最美志愿者管理Controller
 */
@Slf4j
@Controller
@Api(tags = "UmsBestVolunteerController", description = "最美志愿者管理")
@RequestMapping("/bestvolunteer")
public class UmsBestVolunteerController {

    @Autowired
    private UmsBestVolunteerService umsBestVolunteerService;

    @ApiOperation(value = "获取单个最美志愿者信息")
    @RequestMapping(value = "/getById", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<UmsBestVolunteer> getById(@RequestParam(value = "id", defaultValue = "") Long id
    ) {
        try {
            UmsBestVolunteer bestVolunteer = umsBestVolunteerService.getById(id);
            return CommonResult.success(bestVolunteer);
        }catch (Exception e){
            log.error(e.getMessage());
            return CommonResult.failed("获取单个最美志愿者信息失败:"+e.getMessage());
        }
    }
    @ApiOperation(value = "删除单个最美志愿者信息")
    @RequestMapping(value = "/delById", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult delById(@RequestParam(value = "id", defaultValue = "") Long id
    ) {
        try {
            umsBestVolunteerService.delById(id);
            return CommonResult.success("删除成功");
        }catch (Exception e){
            log.error(e.getMessage());
            return CommonResult.failed("删除单个最美志愿者信息失败:"+e.getMessage());
        }
    }

    @ApiOperation(value = "保存最美志愿者信息")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<String> save(@RequestBody UmsBestVolunteer bestVolunteer, BindingResult result) {
        CommonResult commonResult;
        try{
            umsBestVolunteerService.save(bestVolunteer);
            commonResult = CommonResult.success("保存成功！");
        }catch (Exception e){
            log.error("保存最美志愿者信息失败："+e.getMessage());
            commonResult = CommonResult.failed("保存最美志愿者信息失败："+e.getMessage());
        }
        return commonResult;
    }

    @ApiOperation("查询最美志愿者列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult list(
            @RequestParam(value = "status", required = false) String status,
            @RequestParam(value = "title", required = false) String title,
            @RequestParam(value = "position", required = false) Integer position,
            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {

        UmsBestVolunteer query = new UmsBestVolunteer();
        if (!StrUtil.isEmpty(status)) {
            query.setStatus(status);
        }
        if (!StrUtil.isEmpty(title)) {
            query.setTitle(title);
        }
        if (position!=null) {
            query.setPosition(position);
        }
        try {
            List<UmsBestVolunteer> list = umsBestVolunteerService.list(query, pageSize, pageNum);
            return CommonResult.success(CommonPage.restPage(list));
        }catch (Exception e){
            log.error("查询最美志愿者列表出错"+e.getMessage());
            return CommonResult.failed("查询最美志愿者列表出错:"+e.getMessage());
        }
    }
}
